package com.example.huixue.mapper;

import com.example.huixue.entity.Admin;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;

@Mapper
public interface AdminMapper {
    
    /**
     * 根据用户名查找管理员
     */
    @Select("SELECT * FROM admins WHERE username = #{username}")
    Admin findByUsername(String username);
    
    /**
     * 根据邮箱查找管理员
     */
    @Select("SELECT * FROM admins WHERE email = #{email}")
    Admin findByEmail(String email);
    
    /**
     * 根据ID查找管理员
     */
    @Select("SELECT * FROM admins WHERE admin_id = #{adminId}")
    Admin findById(Integer adminId);
    
    /**
     * 插入新管理员
     */
    @Insert("INSERT INTO admins (username, password, email, status, created_at) " +
            "VALUES (#{username}, #{password}, #{email}, #{status}, #{createdAt})")
    @Options(useGeneratedKeys = true, keyProperty = "adminId")
    int insert(Admin admin);
    
    /**
     * 更新管理员信息
     */
    @Update("UPDATE admins SET username = #{username}, email = #{email}, " +
            "status = #{status}, updated_at = NOW() WHERE admin_id = #{adminId}")
    int update(Admin admin);
    
    /**
     * 更新管理员密码
     */
    @Update("UPDATE admins SET password = #{password}, updated_at = NOW() WHERE admin_id = #{adminId}")
    int updatePassword(@Param("adminId") Integer adminId, @Param("password") String password);
    
    /**
     * 更新最后登录时间
     */
    @Update("UPDATE admins SET last_login_time = #{lastLoginTime}, updated_at = NOW() WHERE admin_id = #{adminId}")
    int updateLastLogin(@Param("adminId") Integer adminId, @Param("lastLoginTime") LocalDateTime lastLoginTime);
    
    /**
     * 删除管理员
     */
    @Delete("DELETE FROM admins WHERE admin_id = #{adminId}")
    int deleteById(Integer adminId);
    
    /**
     * 检查是否存在管理员
     */
    @Select("SELECT COUNT(*) FROM admins")
    long countAdmins();
} 